<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class NewsController extends BaseController {

    //新闻列表
	public function index() {
        $data = array_filter($_GET);
        if (!empty($_GET['news_title'])) {
            $data['news_title'] = array('like',"%".trim($_GET['news_title'])."%");
        }
        unset($data['p']);
        $count = M('news')->where($data)->count();
        $page = getpage($count,10);
        $news_arr = M('news')->where($data)->order('news_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('news_arr', $news_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //新闻详情
    public function news_details(){
        $data['news_id'] = I('get.id');
        $news_arr = M('news')->where($data)->find();
        $this->assign('news_arr',$news_arr);
        $this->display();
    }

    //添加新闻
    public function news_add(){
        if ($_POST['action'] == 'news_add_action') {
            unset($_POST['action']);
            if ($_POST != array_filter($_POST)) {
                $this->error('选项不能为空！');
            }
            $finfo = upload($_FILES['news_pic']);
            if ($finfo['status'] == 0) {
                if ($finfo['msg'] == 'nofile') {
                    $this->error('新闻封面图未上传');
                }
                $this->error($finfo['msg']);
            } elseif ($finfo['status'] == 1) {
                $_POST['news_pic'] = $finfo['filepath'];
            }
            $_POST['news_createtime'] = time();
            $filepath = "./Html/news/";
            create_folders($filepath);
            $filename = uniqid().'.html';
            file_put_contents($filepath.$filename,"<title>".$_POST['news_title']."</title><style>body{padding:0 8vw}</style>".$_POST['news_content']);
            $_POST['news_url'] = substr($filepath.$filename, 1);
            unset($_POST['news_content']);
            if (M('news')->add($_POST)) {
                $this->success('操作完成','index');
            }else{
                $this->error('添加新闻失败！');
            }
            exit();
        }
        $this->display();
    }

    //修改新闻
    public function news_edit(){
        if (I('post.action') == 'news_edit_action') {
            $this->news_edit_action();
            exit();
        }
        $data['news_id'] = I('get.id');
        $news_arr = M('news')->where($data)->find();
        $news_arr['news_content'] = file_get_contents(substr($news_arr['news_url'], 1));
        $this->assign('news_arr',$news_arr);
        $this->display();
    }

    //修改新闻操作
    public function news_edit_action(){
        if($_POST != array_filter($_POST)){
            $this->error('新闻选项不能有空值！');
        }
        unset($_POST['action']);
        $finfopic = upload($_FILES['news_pic']);
        //新闻封面图上传
        if ($finfopic['status'] == 0 && $finfopic['msg'] != 'nofile') {
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['news_pic'] = $finfopic['filepath'];
        }
        file_put_contents('.'.$_POST['news_url'],"<title>".$_POST['news_title']."</title><style>body{padding:0 8vw}</style>".$_POST['news_content']);
        unset($_POST['news_content']);
        if(M('news')->save($_POST)){
            $this->success('修改新闻成功','index');
        }else{
            $this->error('新闻没有被修改');
        }
    }

    public function news_del(){
        $data['news_id'] = I('get.id',1);
        M('news')->data($data)->delete();
        $this->redirect('index');
    }


}